package com.simba.server.commands.impl.fetch;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.components.data.BoxInfo;
import com.simba.server.components.data.SortInfo;

public class FetchPacketByBoxCodeCmd implements ICommand
{
	public static final String NAME = "FetchPacketByBoxCodeCmd";
	private final BoxInfo boxInfo;
	private final String sql = "select * from tt_packet_off_info where uploadFlag != -2 and boxCode=?;";

	public FetchPacketByBoxCodeCmd(BoxInfo boxInfo)
	{
		this.boxInfo = boxInfo;
	}

	@Override
	public String getName()
	{
		return FetchPacketByBoxCodeCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);
			dbOperator.setString(1, this.boxInfo.getBoxCode());

			ResultSet rs = dbOperator.executeQuery();

			List<SortInfo> sortInfos = new ArrayList<>();
			while (rs.next())
			{
				SortInfo info = new SortInfo();
				info.setSortResultId(rs.getInt("id"));
				info.setBarcode(rs.getString("barcode"));
				info.setSorterId(rs.getString("sorterId"));
				info.setDroppedSlotId(rs.getInt("droppedSlot"));
				info.setBoxCode(rs.getString("boxCode"));
                info.setPlanId(rs.getString("planId"));
				info.setTurnNumber(rs.getInt("turnNumber"));
				info.setTimePacketOff(rs.getString("packetOffTime"));
				info.setChargeWeight(rs.getString("weight"));
				info.setRoadSlot(rs.getString("roadSlot"));
				info.setLength(rs.getString("length"));
				info.setWidth(rs.getString("width"));
				info.setHeight(rs.getString("height"));
				info.setVolume(rs.getString("volume"));
				sortInfos.add(info);
			}
			this.boxInfo.setMails(sortInfos);
			ret = true;
		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}

}
